// EXMsgBox.cpp : implementation file
//

#include "stdafx.h"
#include "EXMsgBox.h"
#include "afxdialogex.h"


// CEXMsgBox dialog

IMPLEMENT_DYNAMIC(CEXMsgBox, CDialog)

CEXMsgBox::CEXMsgBox(CString strText,
                       CString strCaption,
                       UINT uType,
                       CWnd* pParent,
                       UINT uPosition/*CENTER_CENTER*/,
                       int nTime/* = 0*/)
                       : CEXDialog(CEXMsgBox::IDD, pParent)
                       , m_strText(strText)
                       , m_strCaption(strCaption)
                       , m_uType(uType)
                       , m_uPosition(uPosition)
                       , m_nTime(nTime)
{
  m_strText.Replace(L"^", L"\n");
  m_pParent = pParent;
}

CEXMsgBox::~CEXMsgBox()
{
}

void CEXMsgBox::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
 DDX_Control(pDX, IDC_BTN_CANCEL, m_btnCancel);
 DDX_Control(pDX, IDC_BTN_NO, m_btnNo);
 DDX_Control(pDX, IDC_BTN_OK, m_btnOk);
 DDX_Control(pDX, IDC_BTN_RETRY, m_btnRetry);
 DDX_Control(pDX, IDC_BTN_YES, m_btnYes);
}


BEGIN_MESSAGE_MAP(CEXMsgBox, CDialog)
  ON_WM_PAINT()
  ON_WM_TIMER()
  ON_WM_NCHITTEST()
  ON_BN_CLICKED(IDC_BTN_OK, &CEXMsgBox::OnBnClickedBtnOk)
  ON_BN_CLICKED(IDC_BTN_CANCEL, &CEXMsgBox::OnBnClickedBtnCancel)
  ON_BN_CLICKED(IDC_BTN_RETRY, &CEXMsgBox::OnBnClickedBtnRetry)
  ON_BN_CLICKED(IDC_BTN_NO, &CEXMsgBox::OnBnClickedBtnNo)
  ON_BN_CLICKED(IDC_BTN_YES, &CEXMsgBox::OnBnClickedBtnYes)
END_MESSAGE_MAP()


// CEXMsgBox message handlers

void CEXMsgBox::SetResSet(CEXBaseResSet *pResSet)
{
  m_pMsgBoxResSet = (CEXMsgBoxResSet *)pResSet;
  
  if(m_pMsgBoxResSet->m_nType == MSGBOX_CTRL)
  {
    m_nImgId = m_pMsgBoxResSet->m_nImgId;
    m_nImg_DivideCnt = m_pMsgBoxResSet->m_nImg_DivideCnt;
    m_nType = m_pMsgBoxResSet->m_nType;
  }

  CEXDialog::SetResSet((CEXDialogResSet *)m_pMsgBoxResSet->m_DialogResSet);
}

void CEXMsgBox::Sound()
{
  if (m_uType & MB_ICONASTERISK)
    MessageBeep(MB_ICONASTERISK);
  else if (m_uType & MB_ICONHAND)
    MessageBeep(MB_ICONHAND);
  else if (m_uType & MB_ICONQUESTION)
    MessageBeep(-1);
  else
    MessageBeep( MB_ICONEXCLAMATION);
}

void CEXMsgBox::SetCaption(CString strCaption)
{
  m_strCaption = strCaption;
}

void CEXMsgBox::SetMessageText(CString strText)
{
  m_strText = strText;
}

void CEXMsgBox::SetType(UINT uType)
{
  m_uType = uType;
}

void CEXMsgBox::SetMsgBoxPosition(UINT uPosition)
{
  m_uPosition = uPosition;
}

void CEXMsgBox::SetMsgBoxTime(int nTime)
{
  m_nTime = nTime;
}

void CEXMsgBox::DrawLayOut()
{
    

  CRect rcDlg;
  rcDlg.SetRectEmpty();
  rcDlg.right = 300;
  rcDlg.bottom = 30;

  // text
  CDC * pDC = GetDC();
  CFont * pFont = pDC->SelectObject(GetDlgFont());

  CRect rcStatic;
  rcStatic.left = 20;
  rcStatic.top = rcDlg.bottom + 20;
  rcStatic.bottom = rcStatic.top;

  CRect rcCalc;
  rcCalc.SetRectEmpty();
  pDC->DrawText(m_strText, &rcCalc, DT_CALCRECT | DT_CENTER);

  rcStatic.right = rcStatic.left + rcCalc.Width();
  rcStatic.bottom = rcStatic.top + rcCalc.Height() + 10;

  if (rcDlg.right < rcStatic.right + 20)
    rcDlg.right = rcStatic.right + 20;
  else
    rcStatic.right = rcDlg.right - 20;

  pDC->SelectObject(pFont);
  ReleaseDC(pDC);

  m_rcText = rcStatic;

  
  CRect rcBtn;
  INT nBtnWidth = 70;
  UINT uType = m_uType & 0x00000007L;

  if (uType == MB_RETRYCANCEL) 
  {
    m_btnOk.GetWindowRect(rcBtn);
    ScreenToClient(rcBtn);

    rcBtn.left = (rcDlg.Width() - nBtnWidth * 2) / 2 - 10;
    rcBtn.right = rcBtn.left + nBtnWidth;
    rcBtn.bottom = 25;
    rcBtn.top = rcStatic.bottom + 10;
    rcBtn.bottom += rcBtn.top;
    rcDlg.bottom = rcBtn.bottom + 10;

    CRect rcBtnNo(rcBtn);
    rcBtnNo.left = rcBtn.right + 20;
    rcBtnNo.right = rcBtnNo.left + nBtnWidth;

    m_btnRetry.MoveWindow(rcBtn);
    m_btnRetry.ShowWindow(SW_SHOW);
    m_btnCancel.MoveWindow(rcBtnNo);
    m_btnCancel.ShowWindow(SW_SHOW);
  } 
  else if (uType == MB_YESNO || uType == MB_OKCANCEL) 
  {
    m_btnOk.GetWindowRect(rcBtn);
    ScreenToClient(rcBtn);

    rcBtn.left = (rcDlg.Width() - nBtnWidth * 2) / 2 - 10;
    rcBtn.right = rcBtn.left + nBtnWidth;
    rcBtn.bottom = 25;
    rcBtn.top = rcStatic.bottom + 10;
    rcBtn.bottom += rcBtn.top;
    rcDlg.bottom = rcBtn.bottom + 10;

    CRect rcBtnNo(rcBtn);
    rcBtnNo.left = rcBtn.right + 20;
    rcBtnNo.right = rcBtnNo.left + nBtnWidth;
    
    if(uType == MB_YESNO)
    {
      m_btnYes.MoveWindow(rcBtn);
      m_btnYes.ShowWindow(SW_SHOW);
      m_btnNo.MoveWindow(rcBtnNo);
      m_btnNo.ShowWindow(SW_SHOW);
    }
    else
    {
      m_btnOk.MoveWindow(rcBtn);
      m_btnOk.ShowWindow(SW_SHOW);
      m_btnCancel.MoveWindow(rcBtnNo);
      m_btnCancel.ShowWindow(SW_SHOW);
    }
  } 
  else if (uType == MB_YESNOCANCEL)
  {
    m_btnOk.GetWindowRect(rcBtn);
    ScreenToClient(rcBtn);

    rcBtn.left = (rcDlg.Width() - nBtnWidth * 3 - 30) / 2;
    rcBtn.right = rcBtn.left + nBtnWidth;
    rcBtn.bottom = 25;
    rcBtn.top = rcStatic.bottom + 10;
    rcBtn.bottom += rcBtn.top;
    rcDlg.bottom = rcBtn.bottom + 10;

    CRect rcBtnNo(rcBtn);
    rcBtnNo.left = rcBtn.right + 15;
    rcBtnNo.right = rcBtnNo.left + nBtnWidth;

    CRect rcBtnCancel(rcBtnNo);
    rcBtnCancel.left = rcBtnNo.right + 15;
    rcBtnCancel.right = rcBtnCancel.left + nBtnWidth;

    m_btnYes.MoveWindow(rcBtn);
    m_btnYes.ShowWindow(SW_SHOW);
    m_btnNo.MoveWindow(rcBtnNo);
    m_btnNo.ShowWindow(SW_SHOW);
    m_btnCancel.MoveWindow(rcBtnCancel);
    m_btnCancel.ShowWindow(SW_SHOW);
  } 
  else// if (m_uType == MB_OK || m_uType == MB_ICONEXCLAMATION)
  {
    m_btnOk.GetWindowRect(rcBtn);
    ScreenToClient(rcBtn);
    nBtnWidth +=10;
    rcBtn.left = (rcDlg.Width() - nBtnWidth) / 2;
    rcBtn.right = rcBtn.left + nBtnWidth;
    rcBtn.bottom = 25;
    rcBtn.top = rcStatic.bottom + 10;
    rcBtn.bottom += rcBtn.top;
    rcDlg.bottom = rcBtn.bottom + 10;

    m_btnOk.MoveWindow(rcBtn);
    m_btnOk.ShowWindow(SW_SHOW);
  }

  m_rcDlg = rcDlg;  
  SetPositionDlg();

  AdjustWindowRect(&rcDlg, GetStyle(), FALSE);
  SetWindowPos(NULL, 0, 0, rcDlg.Width(), rcDlg.Height(), SWP_NOZORDER
    | SWP_NOMOVE);

  CEXDialog::SetDialogRegion(this);
}

void CEXMsgBox::SetPositionDlg()
{
  CRect rcParent, rcDlg;
  m_pParent->GetWindowRect(rcParent);

  switch(m_uPosition)
  {
  case CENTER_CENTER:
    rcDlg.left = rcParent.left + (rcParent.Width() / 2) - (m_rcDlg.Width() / 2);
    rcDlg.top = rcParent.top + (rcParent.Height() / 2) - (m_rcDlg.Height() / 2);
    break;

  case CENTER_TOP:
    rcDlg.left = rcParent.left + (rcParent.Width() / 2) - (m_rcDlg.Width() / 2);
    rcDlg.top = rcParent.top + (rcParent.Height() / 20);
    break;

  case CENTER_LEFT:
    rcDlg.left = rcParent.left + 20;
    rcDlg.top = rcParent.top + (rcParent.Height() / 2) - (m_rcDlg.Height() / 2);   
    break;

  case CENTER_RIGHT:
    rcDlg.left = rcParent.left + rcParent.Width() - m_rcDlg.Width() - 20;
    rcDlg.top = rcParent.top + (rcParent.Height() / 2) - (m_rcDlg.Height() / 2);  
    break;

  case CENTER_BOTTOM:
    rcDlg.left = rcParent.left + (rcParent.Width() / 2) - (m_rcDlg.Width() / 2);
    rcDlg.top = rcParent.top + (rcParent.Height() / 20 * 19) - m_rcDlg.Height(); 
    break;
  }
  
  MoveWindow(rcDlg);
}

void CEXMsgBox::OnPaint()
{
  CPaintDC dc(this); // device context for painting
  
  CRect rcClient;
  GetClientRect(rcClient);

  CDC dcBkgnd;
  dcBkgnd.CreateCompatibleDC(&dc);

  CBitmap bmpBkgnd;
  bmpBkgnd.CreateCompatibleBitmap(&dc, rcClient.Width(), rcClient.Height());

  CBitmap * pOldBmpBkgnd = NULL;
  pOldBmpBkgnd = dcBkgnd.SelectObject(&bmpBkgnd);

  CEXDialog::DrawDialog(this, &dcBkgnd);

  
  dc.BitBlt(0, 0, rcClient.Width(), rcClient.Height(), &dcBkgnd, 0, 0,
    SRCCOPY);
  dcBkgnd.SelectObject(pOldBmpBkgnd);

  dc.SetBkColor(GetBKColor());
  dc.SetTextColor(GetTextColor());

  CFont * pFont;
  pFont = dc.SelectObject(GetDlgFont());
  dc.DrawText(m_strText, m_rcText, DT_CENTER | DT_VCENTER);
  dc.SelectObject(pFont);

}


void CEXMsgBox::OnTimer(UINT_PTR nIDEvent)
{
  switch(nIDEvent)
  {
  case IDT_AUTO_CLOSE :
    KillTimer(IDT_AUTO_CLOSE);
    PostMessage(WM_CLOSE);
    break;
  }

  CEXDialog::OnTimer(nIDEvent);
}


BOOL CEXMsgBox::OnInitDialog()
{
  CEXDialog::OnInitDialog();

  m_btnOk.ShowWindow(SW_HIDE);
  m_btnYes.ShowWindow(SW_HIDE);
  m_btnNo.ShowWindow(SW_HIDE);
  m_btnCancel.ShowWindow(SW_HIDE);
  m_btnRetry.ShowWindow(SW_HIDE);

  if(m_nType == MSGBOX_CTRL)
  {
    m_btnOk.SetBgBitmaps(m_nImgId, m_nImg_DivideCnt, 4, 4, 4, 4);
    m_btnOk.SetTextFont(CEXDialog::GetDlgFont());
    m_btnOk.SetBkgndColor(CEXDialog::GetBKColor());

    m_btnCancel.SetBgBitmaps(m_nImgId, m_nImg_DivideCnt, 4, 4, 4, 4);
    m_btnCancel.SetTextFont(CEXDialog::GetDlgFont());
    m_btnCancel.SetBkgndColor(CEXDialog::GetBKColor());

    m_btnNo.SetBgBitmaps(m_nImgId, m_nImg_DivideCnt, 4, 4, 4, 4);
    m_btnNo.SetTextFont(CEXDialog::GetDlgFont());
    m_btnNo.SetBkgndColor(CEXDialog::GetBKColor());

    m_btnRetry.SetBgBitmaps(m_nImgId, m_nImg_DivideCnt, 4, 4, 4, 4);
    m_btnRetry.SetTextFont(CEXDialog::GetDlgFont());
    m_btnRetry.SetBkgndColor(CEXDialog::GetBKColor());

    m_btnYes.SetBgBitmaps(m_nImgId, m_nImg_DivideCnt, 4, 4, 4, 4);
    m_btnYes.SetTextFont(CEXDialog::GetDlgFont());
    m_btnYes.SetBkgndColor(CEXDialog::GetBKColor());
  }
  

  m_btnOk.SetWindowText(L"OK");
  m_btnYes.SetWindowText(L"YES");
  m_btnNo.SetWindowText(L"NO");
  m_btnCancel.SetWindowText(L"CANCEL");
  m_btnRetry.SetWindowText(L"RETRY");

  DrawLayOut();

  CEXDialog::SetCaption(m_strCaption);

  Sound();

  if (m_nTime)
    SetTimer(IDT_AUTO_CLOSE, m_nTime, NULL);

  return TRUE;  // return TRUE unless you set the focus to a control
  // EXCEPTION: OCX Property Pages should return FALSE
}


BOOL CEXMsgBox::PreTranslateMessage(MSG* pMsg)
{
  if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
    return TRUE;
  else if (pMsg->message == WM_SYSKEYDOWN && pMsg->wParam == VK_F4)
    return TRUE;
  else if (pMsg->message == WM_KEYDOWN)
  {
    switch (pMsg->wParam) 
    {
      case VK_RETURN:
      case VK_SPACE:
        if (m_btnOk.IsWindowVisible()) 
        {
          OnBnClickedBtnOk();
          return TRUE;
        } 
        else if (m_btnRetry.IsWindowVisible()) 
        {
          OnBnClickedBtnRetry();
          return TRUE;
        } 
        else if (m_btnCancel.IsWindowVisible()) 
        {
          OnBnClickedBtnCancel();
          return TRUE;
        } 
        else if (m_btnYes.IsWindowVisible()) 
        {
          OnBnClickedBtnYes();
          return TRUE;
        }     
    }
  }
  else if (pMsg->message == WM_CHAR) 
  {
    if (pMsg->wParam == L'Y' || pMsg->wParam == L'y') 
    {
      if (m_btnYes.IsWindowVisible()) 
      {
        OnBnClickedBtnYes();
        return TRUE;
      }
    } 
    else if (pMsg->wParam == L'N' || pMsg->wParam == L'n') 
    {
      if (m_btnNo.IsWindowVisible()) 
      {
        OnBnClickedBtnNo();
        return TRUE;
      }
    } 
    else if (pMsg->wParam == L'R' || pMsg->wParam == L'r') 
    {
      if (m_btnRetry.IsWindowVisible()) 
      {
        OnBnClickedBtnRetry();
        return TRUE;
      }
    } 
    else if (pMsg->wParam == L'C' || pMsg->wParam == L'c') 
    {
      if (m_btnCancel.IsWindowVisible()) 
      {
        OnBnClickedBtnCancel();
        return TRUE;
      }
    } 
    else if (pMsg->wParam == L'O' || pMsg->wParam == L'o') 
    {
      if (m_btnOk.IsWindowVisible()) 
      {
        OnBnClickedBtnOk();
        return TRUE;
      }
    }
  }

  return CEXDialog::PreTranslateMessage(pMsg);
}


LRESULT CEXMsgBox::OnNcHitTest(CPoint point)
{
  LRESULT nRet = CEXDialog::OnNcHitTest(point);

  return nRet;
}


void CEXMsgBox::OnBnClickedBtnOk()
{
  m_uReturnValue = IDOK;
  CDialog::OnOK();
}


void CEXMsgBox::OnBnClickedBtnCancel()
{
  m_uReturnValue = IDCANCEL;
  CDialog::OnCancel();
}


void CEXMsgBox::OnBnClickedBtnRetry()
{
  m_uReturnValue = IDRETRY;
  CDialog::OnOK();
}


void CEXMsgBox::OnBnClickedBtnNo()
{
  m_uReturnValue = IDNO;
  CDialog::OnOK();
}


void CEXMsgBox::OnBnClickedBtnYes()
{
  m_uReturnValue = IDYES;
  CDialog::OnOK();
}
